package com.team.appinfo.controller;

import com.team.appinfo.entity.BackendUser;
import com.team.appinfo.mapper.BackendUserMapper;
import com.team.appinfo.service.BackendUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class BackendUserController {

    @Autowired
    private BackendUserService backendUserService;

    @RequestMapping("/dologin")
    public String dologin(String uresname,String inputCode, String password, HttpSession session, Model model){
        String randCode=session.getAttribute("codeValidate").toString();
        if (inputCode.equalsIgnoreCase(randCode)){
            BackendUser bu = this.backendUserService.dologin(uresname, password);

            if (bu==null){
                model.addAttribute("error","登陆错误");
                return "backendlogin";
            }else {
                session.setAttribute("bu",bu);
                session.setMaxInactiveInterval(18000);
                return "backend/main";
            }
        }else {
            model.addAttribute("error","验证码不正确!");
            return "backendlogin";   //返回登入页
        }

    }
    @RequestMapping("/dologout")
    public String dologout(HttpSession session){
        session.removeAttribute("du");
        return "redirect:jsp/backendlogin.jsp";
    }

}
